package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.pojo.Order;
import com.pojo.Ticket;
import com.service.FlightService;
import com.service.OrderService;
import com.service.TicketService;
import com.service.impl.FlightServiceImpl;
import com.service.impl.OrderServiceImpl;
import com.service.impl.TicketServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机票管理
 */
@WebServlet("/ticket")
public class TicketController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端执行操作
        String method = req.getParameter("method");
        HttpSession session = req.getSession();
        resp.setContentType("text/html;charset=utf-8");
        TicketService ticketService = new TicketServiceImpl();
        OrderService orderService = new OrderServiceImpl();
        switch (method) {
            case "selectAll": {//查询所有机票
                String page = req.getParameter("page");
                String limit = req.getParameter("limit");
                List<Ticket> list = ticketService.selectAllTicket(page, limit);
                Map<String,Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "");
                map.put("count", ticketService.selectAllTicketCount());
                map.put("data", list);
                resp.getWriter().write(JSONObject.toJSON(map).toString());//向前端发送数据
                break;
            }
            case "companySelectTicket": {//公司查询机票
                String startAdd = req.getParameter("startAdd");
                String username = req.getParameter("username");
                String createTime = req.getParameter("createTime");
                String page = req.getParameter("page");
                String limit = req.getParameter("limit");
                List<Ticket> list = ticketService.companySelectTicket(username, startAdd, createTime, page, limit);
                Map<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "");
                map.put("count", ticketService.selectTicketCountByCondition(username,startAdd,createTime));
                map.put("data", list);
                resp.getWriter().write(JSONObject.toJSON(map).toString());//向前端发送数据
            }
        }
    }
}
